# -*- coding:utf-8 -*-

import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib
import urllib.parse
import time
import timedelta
import datetime
import requests
from common.my_time import MyTime
from common.attr_v3 import AttrV3
from database.db_handler import MysqlHander
from database.db_business import DbBusiness
import json
import os
from selenium import webdriver

from webdriver_base import WebdriverBase
from common.my_file import MyFile

'''
上交所监管数据
'''


class ShZhishuRelationData(WebdriverBase):
    def __init__(self, file):
        self.file = file
        self.business = DbBusiness()
        self.db = MysqlHander("./config.ini")
        
        self.file_relation = open('./out/shangjiaosuo_zhishu_relation.txt', 'a+', encoding = 'utf-8')
        WebdriverBase.__init__(self, './log/' + os.path.split(__file__)[-1].split(".")[0] + ".log")

    def __del__(self):
        self.file_relation.close()
        pass

    def query_page_data(self):
        try:
            self.add_query_cnt()
            tbody = self.driver.find_element_by_tag_name("tbody")
            trs = tbody.find_elements_by_tag_name("tr")
            for tr in trs:
                tds = tr.find_elements_by_tag_name("td")
                for td in tds:
                    tag_list = td.text.replace("(","|").replace(")","|").split("|")
                    if len(tag_list) != 3:
                        continue

                    code = tag_list[1]

                    if code not in self.stock_list:
                        continue

                    self.file_relation.write(code + "|0|2001|" + self.attr + "|0|" + "\n")
        except Exception as e:
            self.add_err_cnt()
            self.write_log(str(e))
            pass    
        return False
        
    def query_data(self, data):
        data_split = data.split("|")
        self.attr = data_split[2]
        self.driver.get(data_split[3])
        time.sleep(3)
        next_page = self.driver.find_elements_by_class_name("alpha-top")
        next_page[1].click()
        time.sleep(5)
        self.query_page_data()
        
    
    def get_data(self):
        self.stock_list = self.business.query_stock_code()
        with open(self.file, 'r', encoding='utf-8') as f:
            self.business = f.readlines()
        i = 1
        for b in self.business:
            print(i)
            '''
            if i <= 115:
                i = i + 1
            else:
                self.query_data(b)
                i = i + 1
            '''
            self.query_data(b)
            i = i + 1
        self.driver.quit()

if __name__ == '__main__':
    d = ShZhishuRelationData("./out/shangjiaosuo_zhishu_list_data.txt")
    d.get_data()
